package ui.list 
{
	import com.GCShip;
	import events.GCEvent;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCShipScrollList extends ScrollList
	{
		private var _ships:Vector.<GCShip>;
		private var _selectedShip:GCShipListUI;
		
		/**
		 * Constructor
		 * @param	width
		 * @param	height
		 * @param	ships
		 */
		public function GCShipScrollList(width:int, height:int, ships:Vector.<GCShip> = null) 
		{
			super(width, height);
			this._ships = ships;
			loadShips(ships);
		}
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------
		
		
		public function loadShips(ships:Vector.<GCShip>):void
		{
			// Remove all previous items
			removeAll();
			this._ships = ships;
			_displayList = new Dictionary();
			
			// Fill the list with all the items in the inventory
			if (_ships != null)
			{
				var ship:GCShip;
				var shipUI:GCShipListUI;
				for each (ship in _ships)
				{
					shipUI = new GCShipListUI(ship);
					addDisplay(shipUI);
				}
			}
		}
		
		/**
		 * Clears out all the items in the list
		 */
		override public function removeAll():void
		{
			super.removeAll();
			_ships = null;
			_displayList = null;
			_selectedShip = null;
		}
		
		
		
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------

		private function addDisplay(newShip:GCShipListUI):void
		{
			// XY Positioning
			newShip.x = verticalPadding;
			newShip.y = size * (verticalPadding + itemHeight);
			
			// Add listeners to the UI
			newShip.addEventListener(MouseEvent.ROLL_OVER, handleRollOver);
			newShip.addEventListener(MouseEvent.ROLL_OUT, handleRollOut);
			newShip.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			newShip.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);		
			
			// Update the list properties
			size++;
			listHeight = size * (verticalPadding + itemHeight) - hitAreaHeight;
			
			// Remove the UI from the list
			_list.addChild(newShip);
			
			// Add the itemUI to the display list			
			_displayList[newShip.reference] = newShip;
			
		}
		
		private function removeDisplay(shipUI:GCShipListUI):void
		{
			if (shipUI == null)
				return;
			
			// Check that the item is from this list
			if (_list != shipUI.parent)
				return;
			
			// Remove the listeners on the item
			shipUI.removeEventListener(MouseEvent.ROLL_OVER, handleRollOver);
			shipUI.removeEventListener(MouseEvent.ROLL_OUT, handleRollOut);
			shipUI.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			shipUI.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			
			// Remove the UI from the display
			_list.removeChild(shipUI);
			
			// Update the list properties
			size--;
			listHeight = size * (verticalPadding + itemHeight) - hitAreaHeight;
			
			// Unselect item
			_selectedShip = null;
			
			// Redraw the list
			realign();
			
			// Remove the item from the display list
			delete _displayList[shipUI.reference];
		}		
		
		
		//----------------------------------------------------------------------
		//	Event Handlers
		//----------------------------------------------------------------------
		
        override protected function handleMouseDown(event:MouseEvent):void
        {
			scroll = false;			
			
			// Change coloring of item
			var object:GCShipListUI = event.currentTarget as GCShipListUI;
			if (_selectedShip != null)
				_selectedShip.deselected();
			_selectedShip = object;
			_selectedShip.selected();
			
			dispatchEvent(new GCEvent(GCEvent.LIST_ITEM_SELECTED_EVENT));
        }
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Returns the currently selected itemUI
		 */
		public function get selectedShip():GCShipListUI
		{
			return _selectedShip;
		}			
		
	}

}